/**
 * 载入项目引导文件
 */
require('./bootstrap');

$(document).ready(() => {
    let tableOptions = utils.bootstraptableOptions;
    //请求后台的URL
    tableOptions.url = page_params.url_list;
    //搜索参数
    tableOptions.queryParams = function (params) {//传递参数（*）
        //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
        var temp = {
            //limit: params.limit,   //页面大小
            //offset: params.offset,
            pageSize:params.pageSize,
            page:params.pageNumber,
            status:$("#status").val(),
            keyword:$("#keyword").val(),
        };
        return temp;
    };
    tableOptions.columns = [
        {field: 'id',title: '#'},
        {field: 'name',title: '用户名称'},
        {field: 'email',title: '邮箱'},
        {
            field: 'roles',title: '角色',
            formatter:function (value, row, index) {
                let labels = [];
                if (row.roles){
                    _.forEach(row.roles, function (value, key) {
                        labels.push('<span class="label label-info">' + value.display_name + '</span>');
                    })
                }
                return labels.join('\r\n');
            }
        },
        {
            field: 'deleted_at',title: '状态',
            formatter:function (value, row, index) {
                if (row.deleted_at){
                    return '<span class="label label-warning">禁用</span>';
                }else{
                    return '<span class="label label-primary">激活</span>';
                }
            }
        },
        {field: 'created_at',title: '创建时间'},
        {
            field: 'operate',
            title: '操作',
            formatter: function (value, row, index) {
                let buttons = [];
                if (page_params.url_edit){
                    buttons.push('<button class="btn btn-primary btn-edit"><i class="fa fa-pencil"></i> 编辑</button>');
                }
                if (page_params.url_editrole){
                    buttons.push('<button class="btn btn-primary btn-editrole"><i class="icon iconfont icon-role"></i> 角色编辑</button>');
                }
                if (row.id != 1 && page_params.url_changestatus){
                    if (row.deleted_at){
                        buttons.push('<button class="btn btn-primary btn-changestatus"><i class="fa fa-unlock-alt"></i> 激活</button>');
                    }else{
                        buttons.push('<button class="btn btn-warning btn-changestatus"><i class="fa fa-lock"></i> 禁用</button>');
                    }
                }
                return buttons.join("\r\n");
            },//自定义方法，添加操作按钮
            events : {
                'click .btn-edit':function (e, value, row, index) {
                    $("#box-form form").resetForm();
                    $("#box-form form").attr('action', page_params.url_edit);
                    $("#box-form input[name='id']").val(row.id);
                    $("#box-form input[name='name']").val(row.name);
                    $("#box-form input[name='email']").val(row.email);
                    $("#box-form input[name='email']").attr('readonly', true);
                    box = layer.open({
                        type: 1,
                        title: '编辑用户',
                        shadeClose: true,
                        anim: 2,
                        skin: 'layui-layer-molv', //加上边框
                        area: ['550px', '400px'], //宽高
                        content: $('#box-form')
                    });
                    return false;
                },
                'click .btn-changestatus': function (e, value, row, index) {
                    if (row.deleted_at){
                        utils.confirm(page_params.url_changestatus,{'id':row.id,'status':1}, (response, data) =>{
                            data = response.data;
                            utils.showToastr(data);
                            if (data.status == 'success') {
                                table.bootstrapTable('refresh');
                            }
                        },'你确定激活该用户么？','激活后可恢复使用');
                    }else{
                        utils.confirm(page_params.url_changestatus,{'id':row.id,'status':0}, (response, data) =>{
                            data = response.data;
                            utils.showToastr(data);
                            if (data.status == 'success') {
                                table.bootstrapTable('refresh');
                            }
                        },'你确定禁用该用户么？','禁用后可再次激活');
                    }
                },
                'click .btn-editrole': function (e, value, row, index) {
                    $("#box-form-role form").resetForm();
                    $('#box-form-role form').attr('action', page_params.url_editrole);
                    $("#box-form-role input[name='user_id']").val(row.id);
                    //加载树结构
                    $('#jstree').jstree({
                        'core': {
                            'data': page_params.data_role
                        },
                        'types': {
                            'default': {
                                'icon': 'fa fa-folder'
                            },
                        },
                        "checkbox": {
                            "keep_selected_style": false
                        },
                        "plugins": ['types', 'checkbox'],
                    });
                    //取消选中jstree的所有节点
                    $("#jstree").jstree('uncheck_all');
                    // 批量选中节点
                    $("#jstree").jstree('check_node', row.roleIds);
                    box = layer.open({
                        type: 1,
                        title: '编辑角色',
                        anim: 2,
                        shadeClose: true, //开启遮罩关闭
                        skin: 'layui-layer-molv', //加上边框
                        area: ['400px', '380px'], //宽高
                        content: $('#box-form-role')
                    });
                }
            }
        },
    ];
    let table = $("#table").bootstrapTable(tableOptions);
    //搜索按钮
    $("#btn-search").on('click', function () {
        table.bootstrapTable('refresh');
    });
    //添加用户
    $("#btn-add").on('click', () => {
        $("#box-form form").resetForm();
        $("#box-form form").attr('action', page_params.url_add);
        $("#box-form input[name='id']").val('');
        $("#box-form input[name='email']").attr('readonly', false);
        box = layer.open({
            type: 1,
            title: '添加用户',
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            skin: 'layui-layer-molv', //加上边框
            area: ['620px', '400px'], //宽高
            content: $('#box-form')
        });
    });
    //提交表单
    $("#box-form .btn-sub").on('click', function () {
        utils.ajaxSubmitForm($("#box-form form"), '', {}, (response, postdata) => {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                layer.close(box);
                table.bootstrapTable('refresh');
            }
        });
    });
    $("#box-form-role .btn-sub").on('click', function () {
        //已选权限
        var checkedNode = $("#jstree").jstree('get_checked');
        utils.ajaxSubmitForm($("#box-form-role form"), '', {roleIds:checkedNode}, (response, postdata) => {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                layer.close(box);
                table.bootstrapTable('refresh');
            }
        });
    });
});